package rush.rushcampus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rush.rushcampus.common.Result;
import rush.rushcampus.dto.FoundPageDto;
import rush.rushcampus.dto.IdlePageDto;
import rush.rushcampus.entity.User;
import rush.rushcampus.service.*;
import rush.rushcampus.vo.ArticleVo;
import rush.rushcampus.vo.DynamicVo;
import rush.rushcampus.vo.FoundAllVo;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@RestController
@RequestMapping("/my")
@Api(tags = "个人信息")
public class MyController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IFoundService foundService;
    @Autowired
    private IIdleService idleService;
    @ApiOperation(value = "获取个人信息id为1（标注关注与粉丝）")
    @GetMapping("/getMyInfo")
    public Result getMyInfo(){
        return Result.success(userService.getMyInfoVo());
    }
    @ApiOperation(value = "获取我的发布：type 0:动态、1:圈子、2:失物 、3:闲置")
    @GetMapping("/getMyPublish")
    public Result getMyPublish(Integer type, Integer page, Integer size){
        if (type == 0){
            return Result.success(articleService.pageList(page,size,null, 0 ,null));
        }
        if (type == 1){
            FoundPageDto foundPageDto = new FoundPageDto();
            foundPageDto.setPage(page);
            foundPageDto.setSize(size);
            foundPageDto.setStatus(0);
            return Result.success(foundService.listFound(foundPageDto));
        }
        if (type == 2){
            IdlePageDto idlePageDto = new IdlePageDto();
            idlePageDto.setPage(page);
            idlePageDto.setSize(size);
            idlePageDto.setStatus(0);
            return Result.success(idleService.listIdleVo(idlePageDto));
        }
        return Result.error("参数错误");
    }
    @ApiOperation(value = "获取我的收藏：type 0:动态（发现和失物招领 ,接口响应的为found和article）、1：集市（闲置）")
    @GetMapping("/getMyStar")
    public Result getMyStar(Integer type, Integer page, Integer size){
        User user = userService.getById(1);
        if (type == 0){
            String starArticle = user.getStarArticle();
            Page<ArticleVo> articleVoPage = articleService.pageList(page,size/2,null, 0 ,starArticle);
            //======
            String starFound = user.getStarFound();
            FoundPageDto foundPageDto = new FoundPageDto();
            foundPageDto.setPage(page);
            foundPageDto.setSize(size/2);
            foundPageDto.setStatus(0);
            foundPageDto.setStar(starFound);
            List<FoundAllVo> foundAllVoList = foundService.listFound(foundPageDto);
            //将articleVoPage,foundAllVoList封装到DynamicVo中
            List<ArticleVo> articleVoList = articleVoPage.getRecords();
            DynamicVo dynamicVo = new DynamicVo();
            dynamicVo.setArticleList(articleVoList);
            dynamicVo.setFoundList(foundAllVoList);
            return Result.success(dynamicVo);
        }
        if (type == 1){
            String starIdle = user.getStarIdle();
            IdlePageDto idlePageDto = new IdlePageDto();
            idlePageDto.setPage(page);
            idlePageDto.setSize(size);
            idlePageDto.setStatus(0);
            idlePageDto.setStar(starIdle);
            return Result.success(idleService.listIdleVo(idlePageDto));
        }
        return Result.error("参数错误");
    }
}
